﻿
function extraInit()
{
    initTree(false, false, false);
}

function selectMonth()
{
    var month = parseInt($("input[name=tbxMonth]").val());
    var year = parseInt($("input[name=tbxYear]").val());
    var day = parseInt($("input[name=tbxDay]").val());
    if (isNaN(month) || isNaN(year))
    {
        var date = new Date();
        month = date.getMonth();
        year = date.getFullYear();
        day = date.getDay();
    }
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    viewWorktimeSheet(jobTitleId, year, month, day, 1);
}

function previousMonth()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    month--;
    if (month < 1)
    {
        year--;
        month = 12;
    }
    viewWorktimeSheet(jobTitleId, year, month, day, 1);
}

function nextMonth()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    month++;
    if (month > 12)
    {
        year++;
        month = 1;
    }
    viewWorktimeSheet(jobTitleId, year, month, day, 1);
}

function previousDay()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    var date = new Date();
    date.setFullYear(year);
    date.setMonth(month-1);
    date.setDate(day-1);
    viewWorktimeSheet(jobTitleId, date.getFullYear(), date.getMonth()+1, date.getDate(), 1);
}

function nextDay()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    var date = new Date();
    date.setFullYear(year);
    date.setMonth(month-1);
    date.setDate(day+1);
    viewWorktimeSheet(jobTitleId, date.getFullYear(), date.getMonth()+1, date.getDate(), 1);
}

function previousYear()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    year--;
    viewWorktimeSheet(jobTitleId, year, month, day, 1);
}

function nextYear()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    year++;
    viewWorktimeSheet(jobTitleId, year, month, day, 1);
}

function selectTreeNode(jobTitleId)
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());
    viewWorktimeSheet(jobTitleId, year, month, day, 1);
}

function selectPage(index)
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    viewWorktimeSheet(jobTitleId, year, month, day, index);
}

function viewWorktimeSheet(jobTitleId, year, month, day, pageIndex)
{
    $("input[name=tbxMonth]").val(month);
    $("input[name=tbxYear]").val(year);
    $("input[name=tbxDay]").val(day);
    $("#hdfMonth").val(month);
    $("#hdfYear").val(year);
    $("#hdfDay").val(day);
    
    var area = mask();
    $.ajax({
	    async : false,
	    type: 'POST',
	    url: "/Worktime/AjaxWorktime.aspx",
	    data : { 
		    "action" : "get_over_worktime_sheet", 
		    "jobTitleId" : jobTitleId,
		    "year" : year,
		    "month" : month,
		    "day" : day,
		    "pageIndex" : pageIndex
	    }, 
	    success : function (r) {
	        if (r.status)
			{
			    var table = $("#worktimeTable");
			    var rows = table.find(".div-row");
			    for (var i=rows.length-1; i>0; i--)
			    {
			        rows.eq(i).remove();
			    }
			    for (var i=0; i<r.list.length; i++)
			    {
			        var htmlRow = "<div class='div-row'>";
                    htmlRow += "<div class='div-cell c0'>";
                    htmlRow += r.list[i].order;
                    htmlRow += "</div>";
                    htmlRow += "<div class='div-cell c1'>";
                    htmlRow += "<span class='edit' onclick='editOverWorktime(this);'></span>";
                    htmlRow += "<span class='remove' onclick='removeOverWorktime(this);'></span>";
                    htmlRow += r.list[i].fullName;
                    htmlRow += "<input type='hidden' name='hdfOverWorktimeId' value='"+r.list[i].overWorktimeId+"'/>";
                    htmlRow += "<input type='hidden' name='hdfAgentId' value='"+r.list[i].agentId+"'/>";
                    htmlRow += "</div>";
                    var name = randomString();
                    htmlRow += "<div class='div-cell c6' style='text-align:center'>"+r.list[i].startDate+"</div>";
                    var startHour = r.list[i].startHour;
                    if (startHour != "")
                    {
                        startHour += ":";
                    }
                    htmlRow += "<div class='div-cell c7' style='text-align:right'>"+startHour+"</div>";
                    htmlRow += "<div class='div-cell c8' style='text-align:left'>"+r.list[i].startMin+"</div>";
                    
                    name = randomString();
                    htmlRow += "<div class='div-cell c6' style='text-align:center'>"+r.list[i].endDate+"</div>";
                    var endHour = r.list[i].endHour;
                    if (endHour != "")
                    {
                        endHour += ":";
                    }
                    htmlRow += "<div class='div-cell c7' style='text-align:right'>"+endHour+"</div>";
                    htmlRow += "<div class='div-cell c8' style='text-align:left'>"+r.list[i].endMin+"</div>";
                    htmlRow += "<div class='div-cell c5' style='text-align:center'>"+r.list[i].total+"</div>";
                    
                    htmlRow += "</div>";
                    table.append(htmlRow);
			    }
			    
			    var divPaging = $("#divPaging");
			    var paging = "";
			    for (var i=1; i<=r.totalPage; i++)
			    {
			        if (r.pageIndex == i)
			        {
			            paging += "<span>"+i+"</span>";
			        }
			        else
			        {
			            paging += "<a href='javascript:void(0);' onclick='selectPage("+i+");'>"+i+"</a>";
			        }
			    }
			    divPaging.html(paging);
			}
	    }
    })
    .always(function() {
	    unmask(area);
	});
}


function exportExcel()
{
    var li = $("a.jstree-clicked").closest("li");
    if (li.length  == 0)
    {
        return;
    }
    $("#hdfJobTitleId").val(li.attr("id"));
}

function updateWorktime()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());

    var table = $("#worktimeTable");
    var rows = table.find(".div-row");
    if (rows.length < 2)
    {
        return;
    }
    
    var postData = "";
    for (var i=1; i<rows.length; i++)
    {
        var agentId = rows.eq(i).find("input[name=hdfAgentId]").val();
    
        var morning = 0;
        var ckbMorning = rows.eq(i).find("input:checked[name^=ckbMorning]");
        if (ckbMorning.length > 0)
        {
            morning = ckbMorning.eq(0).val();
        }
        
        var afternoon = 0;
        var ckbAfternoon = rows.eq(i).find("input:checked[name^=ckbAfternoon]");
        if (ckbAfternoon.length > 0)
        {
            afternoon = ckbAfternoon.eq(0).val();
        }
        
        if (postData != "")
        {
            postData += ",";
        }
        postData += agentId + "|" + morning + "|" + afternoon;
    }
    
    var area = mask();
    $.ajax({
	    async : false,
	    type: 'POST',
	    url: "/Worktime/AjaxWorktime.aspx",
	    data : { 
		    "action" : "update_daily_worktime_sheet", 
		    "year" : year,
		    "month" : month,
		    "day" : day,
		    "postData" : postData
	    }, 
	    success : function (r) {
	        if (r.status)
			{
                
            }
            else
            {
                if (r.error != "")
                {
                    alert(r.error);
                }
            }
        }
    })
    .always(function() {
	    unmask(area);
	});
}

function editOverWorktime(button)
{
    var row = $(button).closest(".div-row");
    var cells = row.find(".div-cell");
    
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());
    var startDate = ff(day)+"/"+ff(month)+"/"+ff(year);
    var endDate = cells.eq(5).text();
    if (endDate == "")
    {
        endDate = startDate;
    }
    var input = createDateSelectTag(year,month,day,1,endDate);
    cells.eq(2).text(startDate);
    cells.eq(5).html(input);
    
    var cbxHour1 = createSelectTag(18, 23, cells.eq(3).text().substring(0, 2));
    var cbxHour2;
    if (endDate == startDate)
        cbxHour2 = createSelectTag(18, 23, cells.eq(6).text().substring(0, 2));
    else
        cbxHour2 = createSelectTag(0, 6, cells.eq(6).text().substring(0, 2));
    cells.eq(3).html(cbxHour1);
    cells.eq(6).html(cbxHour2);
    
    var cbxMin1 = createSelectTag(0, 59, cells.eq(4).text());
    var cbxMin2 = createSelectTag(0, 59, cells.eq(7).text());
    cells.eq(4).html(cbxMin1);
    cells.eq(7).html(cbxMin2);
    
    calculate(cells);
    
    cells.eq(1).find("span[class=edit]").attr("onclick", "updateOverWorktime(this);");
}

function updateOverWorktime(button)
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());

    var row = $(button).closest(".div-row");
    var cells = row.find(".div-cell");
    if ((cells.eq(8).text().indexOf("-") >= 0) || (cells.eq(8).text()=="00:00"))
    {
        alert(invalidDateStartEndMessage);
        return;
    }
    
    var agentId = row.find("input[name=hdfAgentId]").val();
    var overWorktimeId = row.find("input[name=hdfOverWorktimeId]").val();
    var endDate = cells.eq(5).find("select").val();
    var startHour = cells.eq(3).find("select").val();
    var startMin = cells.eq(4).find("select").val();
    var endHour = cells.eq(6).find("select").val();
    var endMin = cells.eq(7).find("select").val();
    var endHour2 = endHour;
    if (cells.eq(5).find("select")[0].selectedIndex == 1)
    {
        endHour2 = parseInt(endHour);
        endHour2 += 24;
        endHour2 += "";
    }
    
    cells.eq(1).find("span[class=edit]").attr("onclick", "editOverWorktime(this);");
    
    var area = mask();
    $.ajax({
	    async : false,
	    type: 'POST',
	    url: "/Worktime/AjaxWorktime.aspx",
	    data : { 
		    "action" : "update_over_worktime", 
		    "agentId" : agentId,
		    "overWorktimeId" : overWorktimeId,
		    "year" : year,
		    "month" : month,
		    "day" : day,
		    "startTime" : startHour+":"+startMin,
		    "endTime" : endHour2 + ":"+endMin
	    }, 
	    success : function (r) {
	        if (r.status)
			{
			    cells.eq(1).find("input[name=hdfOverWorktimeId]").val(r.overWorktimeId);
                cells.eq(5).text(endDate);
                cells.eq(3).text(startHour+":");
                cells.eq(4).text(startMin);
                cells.eq(6).text(endHour+":");
                cells.eq(7).text(endMin);
            }
            else
            {
                if (r.error != "")
                {
                    alert(r.error);
                }
            }
        }
    })
    .always(function() {
	    unmask(area);
	});
}

function removeOverWorktime(button)
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());

    var row = $(button).closest(".div-row");
    var cells = row.find(".div-cell");
    var overWorktimeId = row.find("input[name=hdfOverWorktimeId]").val();
    if (overWorktimeId == "")
    {
        emptyRow(cells);
        return;
    }
    
    var area = mask();
    $.ajax({
	    async : false,
	    type: 'POST',
	    url: "/Worktime/AjaxWorktime.aspx",
	    data : { 
		    "action" : "remove_over_worktime",
		    "overWorktimeId" : overWorktimeId,
		    "year" : year,
		    "month" : month,
		    "day" : day
	    }, 
	    success : function (r) {
	        if (r.status)
			{
			    cells.eq(1).find("input[name=hdfOverWorktimeId]").val("");
			    emptyRow(cells);
            }
            else
            {
                if (r.error != "")
                {
                    alert(r.error);
                }
            }
        }
    })
    .always(function() {
	    unmask(area);
	});
}

function emptyRow(cells)
{
    cells.eq(2).text("");
    cells.eq(3).text("");
    cells.eq(4).text("");
    cells.eq(5).text("");
    cells.eq(6).text("");
    cells.eq(7).text("");
    cells.eq(8).text("");
    cells.eq(1).find("span[class=edit]").attr("onclick", "editOverWorktime(this);");
}

function createSelectTag(start, end, selectedValue)
{
    var cbx = "<select onchange='selectDate(this);'>";
    for (var i=start; i<=end; i++)
    {
        var selected = "";
        if (i == selectedValue)
        {
            selected = "selected";
        }
        var strIndex = i + "";
        if (strIndex.length < 2)
        {
            strIndex = "0" + strIndex;
        }
        cbx += "<option "+selected+">"+strIndex+"</option>";
    }
    cbx += "</select>";
    return cbx;
}

function createDateSelectTag(year, month, day, more, selectedValue)
{
    var cbx = "<select onchange='selectDate(this);'>";
    var date = new Date();
    date.setFullYear(year);
    date.setMonth(month-1);
    for (var i=0; i<=more; i++)
    {
        date.setDate(day+i);
        
        var sD = date.getDate() + "";
        if (sD.length < 2)
        {
            sD = "0" + sD;
        }
        var sM = (date.getMonth()+1) + "";
        if (sM.length < 2)
        {
            sM = "0" + sM;
        }
        var value = sD+"/"+sM+"/"+date.getFullYear();
        var selected = "";
        if (value == selectedValue)
        {
            selected = "selected";
        }
        cbx += "<option "+selected+">"+value+"</option>";
    }
    cbx += "</select>";
    return cbx;
}

function selectDate(select)
{
    var row = $(select).closest(".div-row");
    var cells = row.find(".div-cell");
    var cbxHour = cells.eq(6).find("select");
    var cbxMin = cells.eq(7).find("select");
    if (cells.eq(5).find("select")[0].selectedIndex==0)
    {
        if (cbxHour.find("option").length == 7)
        {
            cbxHour = createSelectTag(18, 23, "");
            cbxMin = createSelectTag(0, 59, "");
            cells.eq(6).html(cbxHour);
            cells.eq(7).html(cbxMin);
        }
    }
    else
    {
        if (cbxHour.find("option").length == 6)
        {
            cbxHour = createSelectTag(0, 6, "");
            cbxMin = createSelectTag(0, 59, "");
            cells.eq(6).html(cbxHour);
            cells.eq(7).html(cbxMin);
        }
    }
    
    calculate(cells);
}

function calculate(cells)
{
    var start = parseInt(cells.eq(3).find("select").val())*60+parseInt(cells.eq(4).find("select").val());
    var hour2 = parseInt(cells.eq(6).find("select").val());
    hour2 += 24*cells.eq(5).find("select")[0].selectedIndex;
    var end = hour2*60+parseInt(cells.eq(7).find("select").val());
    var totalMin = (end-start)%60 + "";
    if (totalMin.length == 1)
    {
        totalMin = "0" + totalMin;
    }
    var totalHour = Math.floor((end-start)/60) + "";
    if (totalHour.length == 1)
    {
        totalHour = "0" + totalHour;
    }
    cells.eq(8).text(totalHour+":"+totalMin);
}

function enter(input, evt)
{
    if (evt.keyCode != 13)
    {
        return false;
    }
    processUpdate(input);
    
    return true;
}

function ff(number)
{
    var result = number + "";
    if (result.length == 1)
    {
        result = "0" + result;
    }
    return result;
}










